package com.swallow.auth.infrastructure.impl.tenant;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.swallow.auth.domain.tenant.model.TenantEntity;
import com.swallow.auth.domain.tenant.repository.ITenantDomainRepository;
import com.swallow.auth.infrastructure.convert.tenant.CvtTenantMapper;
import com.swallow.auth.infrastructure.mysql.mapper.TenantMapper;
import com.swallow.auth.infrastructure.mysql.po.TenantPO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author: 18310740596@163.com
 * @date: 2024-05-11 21:48:44
 * @version: v1.0
 * @describe: class responsibility
 */
@Service
public class TenantDomainRepositoryImpl implements ITenantDomainRepository {

    @Resource
    TenantMapper tenantMapper;

    @Override
    public Boolean repeatCheck(TenantEntity entity) {
        Long count = tenantMapper.selectCount(Wrappers.lambdaQuery(TenantPO.class)
                                                      .eq(TenantPO::getTenantName, entity.getTenantName())
                                             );
        return Objects.nonNull(count) && count >= 1;
    }

    @Override
    public void createTenant(TenantEntity entity) {
        TenantPO tenantPO = CvtTenantMapper.INSTANCE.tenantEntity2PO(entity);
        tenantMapper.insert(tenantPO);
    }
}
